//
//  ExampleSymbolView.swift
//  SwiftUIFly
//
//  Created by shen on 2023/10/18.
//

import SwiftUI

struct ExampleSymbolView: View {
    @State var searchText = ""
    @State var symbolString: String = ""
    @State var symbolItems: [SymbolLibrary.Symbol] = []

    var body: some View {
        Group {
            HStack(spacing: 20) {
                searchBarView
            }

            if searchText.isEmpty {
                ForEach(PageIconCategory.allCases, id: \.self) { category in
                    categorySection(category: category)
                }
            } else {
                symbolList(symbols: symbolItems)
            }
        }
    }

    let gridItem = GridItem(.adaptive(minimum: 40), spacing: 4, alignment: .center)

    private func categorySection(category: PageIconCategory) -> some View {
        Section {
            symbolList(symbols: SymbolLibrary.shared.categorySymbols(categoryID: category.rawValue))
        } header: {
            Label {
                category.labelText
            } icon: {
                Image(systemName: category.labelIcon)
            }
        }
    }

    private func symbolList(symbols: [SymbolLibrary.Symbol]) -> some View {
        LazyVGrid(
            columns: [gridItem],
            alignment: .center,
            spacing: 4
        ) {
            ForEach(symbols) { symbol in
                ZStack {
                    RoundedRectangle(cornerRadius: 4).fill(.background)
                    if symbol.id == symbolString {
                        Image(systemName: symbol.id).foregroundStyle(.tint)
                        RoundedRectangle(cornerRadius: 4).strokeBorder(.tint, lineWidth: 2)
                    } else {
                        Image(systemName: symbol.id)
                    }
                }
                .imageScale(.large)
                .aspectRatio(1, contentMode: .fit)
                .onTapGesture {
                    logger.info("symbol: \(symbol.id)")
                    symbolString = symbol.id
                    #if os(macOS)
                    NSPasteboard.general.string = symbolString
                    #else
                    UIPasteboard.general.string = symbolString
                    #endif
                }
            }
        }

    }

    private var searchBarView: some View {
        TextField("搜索Symbol", text: $searchText)
            .padding(8)
            .padding(.horizontal, 25)
            .background(Color(.systemGray6))
            .cornerRadius(8)
            .overlay(Image(systemName: "magnifyingglass")
                .foregroundColor(.gray)
                .frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
                .padding(.leading, 8)
            )
            .padding(.horizontal, 10)
            .onChange(of: searchText) { _ in
                searchSymbol()
            }
    }

    func searchSymbol() {
        let query = searchText.lowercased()
        if query.isEmpty {
            return
        }
        DispatchQueue.global(qos: .background).async {
            let filter = SymbolLibrary.shared.allSymbols.filter { $0.id.lowercased().contains(query) }
            DispatchQueue.main.async {
                withAnimation(.spring()) {
                    self.symbolItems = filter
                }
            }
        }
    }
}
